﻿
var enterPatientsApi = TriageUrl + "/sign/manualEntering";
var editPatientApi = TriageUrl + "/sign/editExaminationInfo";
var loadPaientInfoApi = TriageUrl + "/sign/getExaminationInfo";
var loadUsedSystemApi = QueueCallUrl + "/QcSystem/checkQcSystemsByTriageId";
var loadQueueListBySysApi = QueueCallUrl + "/QcQueue/getNoPageQueueBySystemId";

// 普通队列可以选择医生
var ajaxDoctorByQueueIdUrl = QueueCallUrl + "/QcQueue/getQueueInfoByQueueId";

var g_paient_id = "";
var g_queue_id = "";
var g_queue_key = "";
var g_systemId = "";

$(function () {

    g_paient_id = GetQueryString("paientId");
    g_queue_id = GetQueryString("queueId");
    g_queue_key = GetQueryString("queueKey");
    g_systemId = GetQueryString("systemId");

    if (g_paient_id != "") {
        $("#input_paientId").prop("readonly", true);
        loadPaientInfo();
    } else {
        $("#input_paientId").prop("readonly", false);
    }
    loadCanUsedSystemList();
    try{
        var gender = localStorage.getItem('gender');
        gender==0?$('#select_sex option[value="0"]').prop('selected',true):$('#select_sex option[value="1"]').prop('selected',true);
    }catch (e) {

    }
});

function endForm() {

    $("#input_paientId").prop("readonly", true);
    $("#input_hospitalNo").prop("readonly", true);
    $("#input_paientName").prop("readonly", true);
    $("#select_sex").prop("readonly", true);
    $("#input_age").prop("readonly", true);

    $("#text_diagnosis").prop("readonly", true);
    $("#select_examinationProject").prop("readonly", true);
    $("#select_examinationCheckpoint").prop("readonly", true);

    $("#select_dept").prop("readonly", true);
    $("#select_applyDoctorName").prop("readonly", true);
    $("#input_applyDate").prop("readonly", true);

}


function loadPaientInfo() {

    $.ajax({
        url: loadPaientInfoApi,
        data: {
            id: g_paient_id
        },
        type: 'get',
        success: function (d) {
            if (JSON.parse(d).status == 0) {

                var dataInfo = JSON.parse(d).data;
                var _examinationInfo = JSON.parse(dataInfo.examinationInfo);
                var _creater= dataInfo.creater;
                $("#input_paientId").val(dataInfo.patientKey);
                $("#input_hospitalNo").val(dataInfo.hospitalizationNo);
                $("#input_paientName").val(dataInfo.patientName);
                $("#select_sex").val(dataInfo.sex);
                if (_examinationInfo != null) {
                    $("#input_age").val(_examinationInfo.age);
                    $("#text_diagnosis").val(_examinationInfo.diagnosis);
                    $("#select_examinationProject").val(_examinationInfo.examinationProject);
                    $("#select_examinationCheckpoint").val(_examinationInfo.examinationCheckpoint);
                    $("#select_dept").val(_examinationInfo.applyDeptName);
                    $("#select_applyDoctorName").val(_examinationInfo.applyDoctorName);
                    $("#input_applyDate").val(_examinationInfo.payTime);
                }
                $('#ipt-doctor').val(dataInfo.doctorId);
                $('#select_doctor,#select_queue,#select_sysName').prop('disabled', true);
                if (_creater!='triage') {
                    endForm();
                } else {
                    $("#input_paientId").prop("readonly", false);
                }
            }
        }
    });
}

function loadCanUsedSystemList() {

    var triageId = getDeviceId();
    var hospitalId = getHospitalId();

    $.ajax({
        url: loadUsedSystemApi,
        data: {
            pageSize: 10,
            triageId: triageId,
            hospitalId: hospitalId
        },
        success: function (d) {
            if (JSON.parse(d).status == 0) {

                var optionHtml = "";
                var dataInfoArr = JSON.parse(d).data;
                for (let i = 0; i < dataInfoArr.length; i++) {
                    var _systemId = dataInfoArr[i].systemId;
                    var _systemName = dataInfoArr[i].systemName;

                    optionHtml += "<option value=" + _systemId + ">" + _systemName + "</option>";
                }

                $("#select_sysName").html(optionHtml);


                $("#select_sysName").find("option[value=" + g_systemId+"]").attr("selected",true)
                queryQueueList();
            }
        }
    });
}

function queryQueueList() {

    $.ajax({
        url: loadQueueListBySysApi,
        data: {
            hospitalId: getHospitalId(),
            systemId: $("#select_sysName").val()
        },
        type: 'get',
        success: function (d) {
            if (JSON.parse(d).status == 0) {

                var dataInfoArr = JSON.parse(d).data;
                var optionHtml = "";
                for (let i = 0; i < dataInfoArr.length; i++) {

                    var _queueId = dataInfoArr[i].queueId;
                    var _queueKey = dataInfoArr[i].queueKey;
                    var _queueName = dataInfoArr[i].queueName;
                    // relationType  1 普通队列 2 专家队列
                    optionHtml += "<option value=" + _queueId + "\2" + _queueKey + " data-type='"+dataInfoArr[i].relationType+"'>" + _queueName + "</option>";
                }

                $("#select_queue").html(optionHtml);

                if (g_queue_id != "") {
                    var optionVal = g_queue_id + "\2" + g_queue_key;
                    $("#select_queue option").each(function () {
                        if ($(this).attr("value") == optionVal) {
                            $(this).attr("selected",true)
                        }
                    });
                }
                changeDocList();
            }
        }
    });
}

function enterOrUpdatePatient(flg,callback) {
    if($("#input_paientName").val()=='') return layer.msg('患者姓名不能为空',{icon:0});
    if($("#input_age").val()=='') return layer.msg('年龄不能为空',{icon:0});
    var docName = $('#select_doctor option:checked').text().trim();
    if(docName==='全部医生') docName = '';
    //录入
    if (flg == 0) {

        $.ajax({
            url: enterPatientsApi,
            type: 'post',
            async: false,
            data: {
                hospitalId: getHospitalId(),
                editor: getLoginUserId(),
                queueId: $("#select_queue").val().split("\2")[0],
                queuingKey: $("#select_queue").val().split("\2")[1],
                patientKey: $("#input_paientId").val(),
                patientCardKey: $("#input_paientId").val(),
                hospitalizationNo: $("#input_hospitalNo").val(),
                patientName: $("#input_paientName").val(),
                sex: $("#select_sex").val(),
                examinationInfo: JSON.stringify({
                    examinationProject: $("#select_examinationProject").val(),
                    examinationCheckpoint: $("#select_examinationCheckpoint").val(),
                    age: $("#input_age").val(),
                    birthDay: "",
                    payTime: $("#input_applyDate").val(),
                    applyDoctorName: $("#select_applyDoctorName").val(),
                    applyDeptName: $("#select_dept").val(),
                    diagnosis: $("#text_diagnosis").val()
                }),
                doctorId:$('#select_doctor option:checked').val(),
                doctorName:docName,
                signDeviceId: getDeviceId()
            },
            success: function (d) {
                var res = JSON.parse(d);
                callback&&callback(res);
                if (res.status == 0) {
                   // layer.msg("录入成功", { icon: 1 });
                    callback&&callback(res);

                } else {
                    callback&&callback(res);
                    // layer.msg("录入失败", { icon: 2 });
                }
            }
        });
    }
        //编辑
    else if (flg == 1) {

        var ret = false;
        $.ajax({
            async: false,
            url: editPatientApi,
            type: 'post',
            data: {
                id: g_paient_id,
                hospitalId: getHospitalId(),
                queueId: $("#select_queue").val().split("\2")[0],
                queuingKey: $("#select_queue").val().split("\2")[1],
                patientKey: $("#input_paientId").val(),
                patientCardKey: $("#input_paientId").val(),
                hospitalizationNo: $("#input_hospitalNo").val(),
                patientName: $("#input_paientName").val(),
                sex: $("#select_sex").val(),
                examinationInfo: JSON.stringify({
                    examinationProject: $("#select_examinationProject").val(),
                    examinationCheckpoint: $("#select_examinationCheckpoint").val(),
                    age: $("#input_age").val(),
                    birthDay: "",
                    payTime: $("#input_applyDate").val(),
                    applyDoctorName: $("#select_applyDoctorName").val(),
                    applyDeptName: $("#select_dept").val(),
                    diagnosis: $("#text_diagnosis").val()
                }),
                doctorId:$('#select_doctor option:checked').val(),
                doctorName:docName,
                signDeviceId: getDeviceId(),
                editor: getLoginUserId()
            },
            success: function (d) {
                var res = JSON.parse(d);
                callback&&callback(res);
            }
        });
        return ret;
    }
    else {

        return true;
    }
}
function changeDocList() {
    var val = $('#select_queue option:checked').val();
    var obj = $('#select_queue option:checked').data();
    var arr = val.split("\2");
    // 1 普通队列 2 专家队列
    // 如果是普通队列，需要选择医生
    if(obj.type==1){
      $('.slt-docs').show();
    }else {
      $('.slt-docs').hide();
    }
    getAllDocs(arr[0]);
}
// 获取医生列表
function getAllDocs(queueId) {
  queueId = queueId || '';
  $.ajax({
    url: ajaxDoctorByQueueIdUrl,
    data: {
      queueId: queueId
    },
    type: 'get',
    success: function (d) {
      var res = JSON.parse(d);
      if (res.status == 0) {
        var arr = res.data.queueDocInfo || [];
        dealDocList(arr,res.data.relationType);
      }
    }
  });
}

/**
 * @description:
 * @param arr   医生列表
 * @param type  1 普通队列 2 专家队列
 * **/
function dealDocList(arr,type) {
    arr = arr || [];
    var frag = '<option value="">全部医生</option>';
    if(type==2) frag = '';
    var docId = $('#ipt-doctor').val()
    for (var i = 0,lens = arr.length; i <lens ; i++) {
        if(docId==arr[i]['docId']){
            frag +='<option selected value="'+arr[i]['docId']+'">'+arr[i]['docName']+'</option>';
        }else {
            frag +='<option value="'+arr[i]['docId']+'">'+arr[i]['docName']+'</option>';
        }
    }
    $('#select_doctor').html(frag);
}
